﻿using ExamPlat.Repositories;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using ExamPlat.ViewModels;
using ExamPlat.Models;
using ExamPlat.DAL;

namespace ExamPlat.Controllers
{
    public class UserInfoController : Controller
    {
        private static IUserInfoRepository userInfoRepository = new UserInfoRepository();
        private static IRoleInfoRepository roleInfoRepository = new RoleInfoRepository();
        // GET: UserInfo
        public ActionResult Index(string loginAccount, string userName)
        {
            try
            {
                if (Request.Cookies["CurUser"] == null)
                {
                    return RedirectToAction("Login", "Login");
                }
                HttpCookie curUser = Request.Cookies["CurUser"];
                if (curUser["RoleID"] != "1")
                {
                    ViewBag.Msg = "你没有权限查看该页面";
                    return View("Error");
                }
                ViewBag.RoleID = curUser["RoleID"];

                IList<ViewUserInfo> userInfos = userInfoRepository.GetUserInfos(loginAccount, userName);
                ViewBag.LoginAccount = loginAccount;
                ViewBag.UserName = userName;
                return View(userInfos);
            }
            catch (Exception ex)
            {
                Log4Helper.Info(ex.Message, ex);
                ViewBag.Msg = ex.Message;
                return View("Error");
            }
        }

        // GET: UserInfo/Create
        public ActionResult Create(string searchAccount, string searchName)
        {
            try
            {
                if (Request.Cookies["CurUser"] == null)
                {
                    return RedirectToAction("Login", "Login");
                }
                HttpCookie curUser = Request.Cookies["CurUser"];
                if (curUser["RoleID"] != "1")
                {
                    ViewBag.Msg = "你没有权限查看该页面";
                    return View("Error");
                }
                ViewBag.RoleID = curUser["RoleID"];

                ViewBag.SearchAccount = searchAccount;
                ViewBag.SearchName = searchName;
                if (TempData["Msg"] != null)
                {
                    ViewBag.Msg = TempData["Msg"];
                }
                return View();
            }
            catch (Exception ex)
            {
                Log4Helper.Info(ex.Message, ex);
                ViewBag.Msg = ex.Message;
                return View("Error");
            }
        }

        // POST: UserInfo/Create
        [HttpPost]
        public ActionResult Create(UserInfo model)
        {
            try
            {
                if (Request.Cookies["CurUser"] == null)
                {
                    return RedirectToAction("Login", "Login");
                }
                HttpCookie curUser = Request.Cookies["CurUser"];
                ViewBag.RoleID = curUser["RoleID"];
                int result = userInfoRepository.AddUserInfo(model);
                var account = Request.Form["searchLoginAccount"];
                var name = Request.Form["searchUserName"];
                if (result == 1)
                {
                    return RedirectToAction("Index", "UserInfo", new { loginAccount = account, userName = name });
                }
                else if (result == -1)
                {
                    TempData["Msg"] = "登录帐号已经存在";
                    return RedirectToAction("Create", "UserInfo", new { searchAccount = account, searchName = name });
                }
                else
                {
                    TempData["Msg"] = "增加失败";
                    return RedirectToAction("Create", "UserInfo", new { searchAccount = account, searchName = name });
                }
            }
            catch (Exception ex)
            {
                Log4Helper.Info(ex.Message, ex);
                TempData["Msg"] = ex.Message;
                return View("Error");
            }
        }

        // GET: UserInfo/Edit/5
        public ActionResult Edit(int id, string searchAccount, string searchName)
        {
            try
            {
                if (Request.Cookies["CurUser"] == null)
                {
                    return RedirectToAction("Login", "Login");
                }
                HttpCookie curUser = Request.Cookies["CurUser"];
                if (curUser["RoleID"] != "1")
                {
                    ViewBag.Msg = "你没有权限查看该页面";
                    return View("Error");
                }
                ViewBag.RoleID = curUser["RoleID"];

                ViewBag.SearchAccount = searchAccount;
                ViewBag.SearchName = searchName;
                if (TempData["Msg"] != null)
                {
                    ViewBag.Msg = TempData["Msg"];
                }
                UserInfo userInfo = userInfoRepository.GetUserInfoByID(id);
                return View(userInfo);
            }
            catch (Exception ex)
            {
                Log4Helper.Info(ex.Message, ex);
                ViewBag.Msg = ex.Message;
                return View("Error");
            }
        }

        // POST: UserInfo/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, UserInfo model)
        {
            try
            {
                if (Request.Cookies["CurUser"] == null)
                {
                    return RedirectToAction("Login", "Login");
                }
                HttpCookie curUser = Request.Cookies["CurUser"];
                ViewBag.RoleID = curUser["RoleID"];
                int result = userInfoRepository.UpdateUserInfo(model);
                var account = Request.Form["searchLoginAccount"];
                var name = Request.Form["searchUserName"];
                if (result == 1)
                {
                    return RedirectToAction("Index", "UserInfo", new { loginAccount = account, userName = name });
                }
                if (result == -1)
                {
                    TempData["Msg"] = "登录帐号已经存在";
                    return RedirectToAction("Edit", "UserInfo", new { id = id, searchAccount = account, searchName = name });
                }
                else
                {
                    TempData["Msg"] = "修改失败";
                    return RedirectToAction("Edit", "UserInfo", new { id = id, searchAccount = account, searchName = name });
                }
            }
            catch (Exception ex)
            {
                Log4Helper.Info(ex.Message, ex);
                TempData["Msg"] = ex.Message;
                return View("Error");
            }
        }

        // POST: UserInfo/Delete/5
        [HttpPost]
        public ActionResult Delete(int id)
        {
            JsonResult result = new JsonResult();
            try
            {
                if (Request.Cookies["CurUser"] == null)
                {
                    return RedirectToAction("Login", "Login");
                }
                HttpCookie curUser = Request.Cookies["CurUser"];
                if (curUser["RoleID"] != "1")
                {
                    result.Data = new { msg = "你没有权限操作" };
                }
                else
                {
                    int tmpResult = userInfoRepository.DelUserInfo(id);
                    if (tmpResult == 1)
                    {
                        result.Data = new { msg = "删除成功" };
                    }
                    else
                    {
                        result.Data = new { msg = "删除失败" };
                    }
                }
            }
            catch (Exception ex)
            {
                Log4Helper.Info(ex.Message, ex);
                result.Data = new { msg = "删除异常" };
            }
            return result;
        }
    }
}
